let app = getApp();
let $api = require('../../config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*价格*/
    price:{},
    type:1,
    hiddenmodalput:true, //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
    wechartAccount:'',
    month_query_times:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHot();
        let self = this;
      let token = wx.getStorageSync('userInfo').token;
      wx.request({
          url:$api.api.surplusSearch,
          method:'GET',
          data: {
              token
          },
          header: {
              'content-type': 'application/json'
          },
          success: function(res) {
              var weixinId = res.data.data.weixinId;
              self.setData({
                  wechartAccount:weixinId
              })
          }
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let id = wx.getStorageSync('userInfo').user_id;
    if (res.from === 'button') {
        return {
            title: '股票估值查询',
            path: `/pages/index/index?shareId=${id}`,
            imageUrl:'../../images/share-img.png'
        }
    }else{
        return {
            title: '股票估值查询',
            path: `/pages/index/index?shareId=${id}`,
            imageUrl:'../../images/share-img.png'
        }
    }
  },
  /*获取热搜*/
  getHot(){
    let token = wx.getStorageSync('userInfo').token;
    let self = this;
    wx.request({
        url:$api.api.hot, 
        method:'GET',
        data: {
            token
        },
        header: {
            'content-type': 'application/json' 
        },
        success: function(res) {
            let data = res.data.data;
            // let hostList = data.hot;
            let queryTotal = data.total_query_times;
            let price = data.price;
            let month_query_times = data.month_query_times;
            self.setData({
                queryTotal:queryTotal,
                // hostList:hostList,
                price:price,
                month_query_times:month_query_times
            })
        }
    })
  },
  selectPay(e){
    let type = e.currentTarget.dataset.type;
    this.setData({
      type
    })
  },
  /*支付*/
  pay(){
    let token = wx.getStorageSync('userInfo').token;
    let type = this.data.type;
    let self = this;
    if(self.data.wechartAccount==''){
        wx.showToast({
            title:'请输入微信号！',
            icon: 'failed',
            duration: 2000
        })
        return false;
    }else{
        wx.request({
            url:$api.api.setWeixinId,
            method:'GET',
            data: {
                token,
                weixinId:self.data.wechartAccount
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                // var weixinId = res.data.weixinId;
                // self.setData({
                //     weixinId:weixinId
                // })
            }
        })
        wx.request({
            url:$api.api.pay,
            method:'GET',
            data: {
                token,
                type
            },
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                let data = JSON.parse(res.data.data);
                wx.requestPayment({
                    'timeStamp':`${data.timeStamp}` ,
                    'nonceStr': `${data.nonceStr}`,
                    'package': `${data.package}`,
                    'signType': 'MD5',
                    'paySign': `${data.paySign}`,
                    'success':function(res){
                        wx.showToast({
                            title: '请输入微信号！',
                            icon: 'none',
                            duration: 2000
                        })
                    },
                    'fail':function(res){
                        wx.showToast({
                            title: '支付失败',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                })
            }
        })
    }

  },
  //微信号
    inpWxFuc(e){
      var self = this;
      var val = e.detail.value;
      self.setData({
          wechartAccount:val
      });
  }
})